package com.hmall.trade.listenew;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xmy
 * @version 1.0
 */
@Slf4j
@Component
public class PayStatusListener {
    @Autowired
    private IOrderService iOrderService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue",durable = "true"),
            exchange = @Exchange(name = "pay.direct"),
            key = "pay.success"
    ))
    public void listenPaySuccess(Long orderId){
        log.info("接收到支付成功的消息，订单id为：{}",orderId);
        //先查定单
        Order order=iOrderService.getById(orderId);
        //判断订单是否为未支付，是才标记
        if(order==null||order.getStatus()!=1){
            //不做处理
            return;
        }
        //标记为已支付
        iOrderService.markOrderPaySuccess(orderId);
    }

}
